<!-- 趋势图表 -->

<template>
  <EasyChart :baseOption="OPTION" :option="option" />
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import CONST from '@admin/components/chart/CONST'

import OPTION from './OPTION'
import props from './props'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'TrendChart',

  /**
   * 属性注册 (抽取以便查阅)
   */
  props,

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 基础配置
     */
    OPTION() {
      const { blue, red, green, golden } = CONST.LINE_SERIE
      const count = this.dataList.length
      let series = []
      if (count === 1) series = [blue]
      if (count === 2) series = [blue, red]
      if (count === 3) series = [golden, green, red]
      if (count === 4) series = [golden, green, red, blue]
      return CORE.merge({}, OPTION, { series })
    },

    /**
     * 图表配置
     */
    option() {
      const { xData, dataList, dataNames, dataUnits } = this
      return {
        xAxis: { data: xData },
        series: dataList.map((data, i) => ({ data, name: dataNames[i] })),
        tooltip: {
          formatter(params) {
            var tip = `<b class="MonitorElec-TrendChart-tip">时间：${params[0].name}</b>`
            params.forEach((param, i) => {
              tip += `<br/>${param.marker} ${param.seriesName}　<b>${param.value}${dataUnits[i]}</b>`
            })
            return tip
          },
        },
      }
    },
  },
}
</script>

<style lang="scss">
@import './index.scss';
</style>
